
#include <pthread.h>

namespace LockModule
{
    class Mutex
    {
    public:
        Mutex(const Mutex&)= delete;
        Mutex& operator=(const Mutex&)= delete;
        Mutex()
        {
            pthread_mutex_init(&_mutex, nullptr);//pthread_mutex_init初始化的互斥量必须使用pthread_mutex_destroy销毁
        } 

        void Lock()
        {
            pthread_mutex_lock(&_mutex);
        }

        void Unlock()
        {
            pthread_mutex_unlock(&_mutex);
        }

        pthread_mutex_t* GetMutex()
        {
            return &_mutex;
        }

        ~Mutex()
        {
            pthread_mutex_destroy(&_mutex);
        }

    private:
        pthread_mutex_t _mutex;
    };


    template<class T>
    class LockGuard
    {
    public:
        LockGuard(T& mutex)
        :_mutex(mutex)
        {
            _mutex.Lock();
        }

        ~LockGuard()
        {
            _mutex.Unlock();
        }
    private:
        T& _mutex;
    };
}
